package com.xr.hr.personnel;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.xr.common.CommUtil;

/**
 * batchEmpImpViewModel
 */
public class batchEmpImpViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset ds=getDataset("datasetBatchEmpImp");
	 /* ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
	   if(ds.getLong("IMP_BATCH_EMP_ID")<=0){
			  ds.setValue("CREATE_ID", CommUtil.getEmployeeId());
			  ds.setValue("IMP_BATCH_EMP_ID", CommUtil.getNewId("SEQ_IMP"));
		  } 
		  ds.moveNext();
	  }*/
	  super.doUpdateData(parameters, outParameters);
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
		  if(ds.getDouble("V1100")>0){
			  dbs.setSql("select count(1) from IMP_BATCH_EMP_ITEMS where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  if(dbs.query().getInt(0)==0){
				  dbs.setSql("insert into IMP_BATCH_EMP_ITEMS(BATCH_EMP_ITEMS_ID,IMP_BATCH_EMP_ID,HRITEMS_ID,MONEY)values(SEQ_IMP.Nextval,"+ds.getString("IMP_BATCH_EMP_ID")+",1100,"+ds.getDouble("V1100")+")");
				  dbs.execute();
			  }else{
				  dbs.setSql("update IMP_BATCH_EMP_ITEMS set money="+ds.getDouble("V1100")+" where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
				  dbs.execute();
			  }
		  }else{
			  dbs.setSql("delete from IMP_BATCH_EMP_ITEMS where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  dbs.execute();
		  }
		  if(ds.getDouble("V1101")>0){
			  dbs.setSql("select count(1) from IMP_BATCH_EMP_ITEMS where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  if(dbs.query().getInt(0)==0){
				  dbs.setSql("insert into IMP_BATCH_EMP_ITEMS(BATCH_EMP_ITEMS_ID,IMP_BATCH_EMP_ID,HRITEMS_ID,MONEY)values(SEQ_IMP.Nextval,"+ds.getString("IMP_BATCH_EMP_ID")+",1101,"+ds.getDouble("V1101")+")");
				  dbs.execute();
			  }else{
				  dbs.setSql("update IMP_BATCH_EMP_ITEMS set money="+ds.getDouble("V1101")+" where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
				  dbs.execute();
			  }
		  }else{
			  dbs.setSql("delete from IMP_BATCH_EMP_ITEMS where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  dbs.execute();
		  } 
		  ds.moveNext();
	  }
	
	  dbs.close();
  }
  public void impAction(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset ds=getDataset("datasetBatchEmpImp");
/*	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
		  if(ds.getLong("IMP_BATCH_EMP_ID")<=0){
			  ds.setValue("CREATE_ID", CommUtil.getEmployeeId());
			  ds.setValue("IMP_BATCH_EMP_ID", CommUtil.getNewId("SEQ_IMP"));
		  }
		  ds.moveNext();
	  }*/
	  super.doUpdateData(parameters, outParameters);
	  String impManId=CommUtil.getEmployeeId();
	  String impManOosId=CommUtil.getOnoffStationId();
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
		  if(ds.getDouble("V1100")>0){
			  dbs.setSql("select count(1) from IMP_BATCH_EMP_ITEMS where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  if(dbs.query().getInt(0)==0){
				  dbs.setSql("insert into IMP_BATCH_EMP_ITEMS(BATCH_EMP_ITEMS_ID,IMP_BATCH_EMP_ID,HRITEMS_ID,MONEY)values(SEQ_IMP.nextnvl,"+ds.getString("IMP_BATCH_EMP_ID")+",1100,"+ds.getDouble("V1100")+")");
				  dbs.execute();
			  }else{
				  dbs.setSql("update IMP_BATCH_EMP_ITEMS set money="+ds.getDouble("V1100")+" where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
				  dbs.execute();
			  }
		  }else{
			  dbs.setSql("delete from IMP_BATCH_EMP_ITEMS where hritems_id=1100 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  dbs.execute();
		  }
		  if(ds.getDouble("V1101")>0){
			  dbs.setSql("select count(1) from IMP_BATCH_EMP_ITEMS where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  if(dbs.query().getInt(0)==0){
				  dbs.setSql("insert into IMP_BATCH_EMP_ITEMS(BATCH_EMP_ITEMS_ID,IMP_BATCH_EMP_ID,HRITEMS_ID,MONEY)values(SEQ_IMP.nextnvl,"+ds.getString("IMP_BATCH_EMP_ID")+",1101,"+ds.getDouble("V1101")+")");
				  dbs.execute();
			  }else{
				  dbs.setSql("update IMP_BATCH_EMP_ITEMS set money="+ds.getDouble("V1101")+" where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
				  dbs.execute();
			  }
		  }else{
			  dbs.setSql("delete from IMP_BATCH_EMP_ITEMS where hritems_id=1101 and IMP_BATCH_EMP_ID="+ds.getString("IMP_BATCH_EMP_ID"));
			  dbs.execute();
		  }
		  dbs.setSql("begin pck_imp.batchEmpImp("+ds.getString("IMP_BATCH_EMP_ID")+","+impManId+","+impManOosId+"); end;");
		  dbs.execute();
		  
		  ds.moveNext();
	  }
	
	  dbs.close();
   }
  
  public void delete(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset ds=getDataset("datasetBatchEmpImp");
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && !ds.getString("STATUS").equals("1")){
			  dbs.setSql("delete from IMP_BATCH_EMP_ITEMS where imp_batch_emp_id="+ds.getString("imp_batch_emp_id"));
			  dbs.execute();
			  dbs.setSql("delete from imp_batch_emp where imp_batch_emp_id="+ds.getString("imp_batch_emp_id"));
			  dbs.execute();
		  }
		  ds.moveNext();
	  }
	  dbs.close();
  }
  
  
  public void BatchEmpLeaveDelete(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset ds=getDataset("datasetBatchLeave");
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && !ds.getString("STATUS").equals("1")){
			  dbs.setSql("delete from IMP_BATCH_LEAVE where IMP_BATCH_LEAVE_ID="+ds.getString("IMP_BATCH_LEAVE_ID"));
			  dbs.execute();
		  }
		  ds.moveNext();
	  }
	  dbs.close();
  }
  
  public void batchLeaveAction(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset ds=getDataset("datasetBatchLeave");
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
		  if(ds.getLong("IMP_BATCH_LEAVE_ID")<=0){
		   ds.setValue("CREATE_ID", CommUtil.getEmployeeId());
		   ds.setValue("IMP_BATCH_LEAVE_ID", CommUtil.getNewId("SEQ_IMP"));
		  }
		  ds.moveNext();
	  }
	  super.doUpdateData(parameters, outParameters);
	  String impManId=CommUtil.getEmployeeId();
	  String impManOosId=CommUtil.getOnoffStationId();
	  ds.moveFirst();
	  while(!ds.isLast()){
		  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
			  ds.setRecordState(Record.STATE_NONE);
			  ds.moveNext();
			  continue;
		  }
		  dbs.setSql("begin pck_imp.batchEmpLeave("+ds.getString("IMP_BATCH_LEAVE_ID")+","+impManId+","+impManOosId+"); end;");
		  dbs.execute();
		  
		  ds.moveNext();
	  }
	
	  dbs.close();
   }
  public void batchLeaveSave(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
  Dataset ds=getDataset("datasetBatchLeave");
  ds.moveFirst();
  while(!ds.isLast()){
	  if(ds.getString("STATUS")!=null && ds.getString("STATUS").equals("1")){
		  ds.setRecordState(Record.STATE_NONE);
		  ds.moveNext();
		  continue;
	  }
	  ds.setValue("CREATE_ID", CommUtil.getEmployeeId());
	  ds.setValue("IMP_BATCH_LEAVE_ID", CommUtil.getNewId("SEQ_IMP"));
	  ds.moveNext();
  }
  super.doUpdateData(parameters, outParameters);
}
  
}
